package com.vincent.sciinfo.service.impl;

import com.vincent.sciinfo.mapper.PaperMapper;
import com.vincent.sciinfo.pojo.Paper;
import com.vincent.sciinfo.pojo.PaperExample;
import com.vincent.sciinfo.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperMapper paperMapper;

    @Override
    public List<Paper> selectPaperByTeacherId(int ope, String teacherId) {
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        criteria.andTeacheridEqualTo(teacherId);
        criteria.andCheckflagEqualTo(ope);
        return paperMapper.selectByExample(paperExample);
    }

    @Override
    @Transactional
    public int insertPaper(Paper paper) {
        //添加论文
        int codeInsert = paperMapper.insertPart(paper);
        //根据论文类型自动评分
        int codeScore = paperMapper.updatePaperScoreByType(paper.getResultuuid(), paper.getPapertypeid());
        return codeScore + codeInsert;
    }


    /**
     * 查询所有通过审核或被下架的论文
     *
     * @return
     */
    @Override
    public List<Paper> selectCheckAndRemovePaper() {
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        List<Integer> vals = new LinkedList<>();
        vals.add(0);
        vals.add(1);
        vals.add(3);
        criteria.andCheckflagIn(vals);
        return paperMapper.selectByExample(paperExample);
    }

    @Override
    public void updatePaperStatus(String paperId, int operaCode) {
        Paper paper = paperMapper.selectByPrimaryKey(paperId);
        paper.setCheckflag(operaCode);
        paperMapper.updateByPrimaryKey(paper);
    }

    @Override
    public String[] selectPaperNameById(String paperId) {
        String[] strs = new String[2];
        Paper paper = paperMapper.selectByPrimaryKey(paperId);
        strs[0] = paper.getTeacherid();
        strs[1] = paper.getPapername();
        return strs;
    }

}
